import json
import time

from django.shortcuts import render
from rest_framework.views import APIView
from qimomo.models import *
import datetime
from rest_framework.response import Response
from base.tasks import send_emailc
from pymysql import connections
import pymysql
from qimomo.serlize import *
from qimomo.redis import r
from django.db.models import Q
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
# Create your views here.

class CommentView(APIView):
    def post(self,request):
        title=request.data.get("title")
        content = request.data.get("content")
        stuid = request.data.get("stuid")
        create_time = request.data.get("create_time")
        com=CommentsModel()
        com.title=title
        com.content=content
        com.stu=StudentModel.objects.filter(id=stuid).first()
        com.create_time=create_time
        com.save()
        return Response({"message":"成功"})
    def delete(self,request,id):
        comment=CommentsModel.objects.filter(id=id).first()
        stuemail=comment.stu.stu_email
        comment.delete()
        send_emailc(stuemail).delay(2)
        return Response({"message":"删除成功"})


    def get(self,reqeust):

        userid=reqeust.GET.get("userid")
        if (r.get_str(userid)):
            datastr=r.get_str(userid)

            return Response({"message": "查询成功", "data":datastr})
        # sql=f'SELECT * FROM `comment` where DATE_FORMAT(create_time,"%m")=DATE_FORMAT(NOW(),"%m") AND stu_id={userid} ORDER BY create_time  '
        # with connections["default"].cursors as cur:
        #     cur.excute(sql)
        #     data=cur.fetchall()
        con=Connections()
        data=con.getinfo(userid)
        r.setex_str(userid,str(data) ,12*60)

        return Response({"message":"查询成功","data":data})

class Comment2View(APIView):
    def get(self,reqeust):
        title=reqeust.GET.get("title")
        pagenumber=int(reqeust.GET.get("page",1))
        pagesize=8
        date2= "2024-07-22 23:00:00"
        date1= "2024-07-15"
        print(date2)
        timelist=[date1,date2]
        q=Q()
        if(title):
            q=Q(content__contains=title)
        list=CommentsModel.objects.filter(q).filter(create_time__range=timelist)

        page=Paginator(list,pagesize)
        try:
            pagelist=page.page(pagenumber)

        except PageNotAnInteger:
            pagelist=page.page(1)
        except EmptyPage:
            pagelist=page.page( page.num_pages)
        com=CommentSer(instance=pagelist.object_list,many=True)
        next=pagelist.has_next()
        previous=pagelist.has_previous()
        return Response({"message": "查询成功", "data": com.data,"next":next,"previous":previous})

class StudentCommenet(APIView):
    def get(self,request):
        con=Connections()
        userid = request.GET.get("userid")
        datalist=con.getstudent(userid)

        return Response({"message": "查询成功", "data": datalist})
class StudentCount(APIView):
    def get(self,request):
        con=Connections()
        datalist=con.getstudentcount()

        return Response({"message": "查询成功", "data": datalist})

class Connections():
    def __init__(self):
        self.con=pymysql.connections.Connection(user="root",password="123456",database="manageproject",port=3306,host="localhost",charset="utf8")
    def getinfo(self,userid):
        cursor=self.con.cursor()
        sql = f'SELECT * FROM `comment` where DATE_FORMAT(create_time,"%m")=DATE_FORMAT(NOW(),"%m") AND stu_id={userid} ORDER BY create_time  '
        cursor.execute(sql)
        data=cursor.fetchall()
        return data

    def getstudent(self,userid):
        cursor = self.con.cursor()
        sql = f'SELECT * FROM `comment` where  stu_id={userid} AND not( content  LIKE "%红包%" or content  LIKE "%微信%") ORDER BY create_time DESC LIMIT 8'
        cursor.execute(sql)
        data = cursor.fetchall()
        return data
    def getstudentcount(self):
        cursor = self.con.cursor()
        sql = 'SELECT stu_name,count(*) FROM comment LEFT JOIN student on `comment`.stu_id=student.id GROUP BY stu_name'
        cursor.execute(sql)
        data = cursor.fetchall()
        return data